package cn.b504.qpcr.impls;

import cn.b504.qpcr.interfaces.Access;
import cn.b504.qpcr.utils.Utils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.*;

@Service
public class AccessImpl implements Access {
  private final String idOfCPUHashCode;
  private final String idOfCPUHashCodeHashCode;

  public AccessImpl(String idOfCPUHashCode, String idOfCPUHashCodeHashCode) {
    this.idOfCPUHashCode = idOfCPUHashCode;
    this.idOfCPUHashCodeHashCode = idOfCPUHashCodeHashCode;
  }

  @Override
  public boolean check() {
    File dir;
    try {
      dir = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return false;
    }
    File lic = new File(dir, "LIC");
    if (!lic.exists() || !lic.isFile() || !lic.canRead()) {
      return false;
    }

    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    try {
      is = new FileInputStream(lic);
      isr = new InputStreamReader(is);
      br = new BufferedReader(isr);
      String line = br.readLine();
      String[] split = line.split("-");
      if (split.length != 2) {
        return false;
      }
      if (!split[0].equals(idOfCPUHashCode)) {
        return false;
      }
      return access(split[1]);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (isr != null) {
        try {
          isr.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return true;
  }

  @Override
  public boolean access(String value) {
    boolean result = false;
    if (value != null) { // 校验
      result = value.equalsIgnoreCase(idOfCPUHashCodeHashCode);
    }
    return result;
  }

  public boolean deviceAccess(String id) {
    File dir;
    try {
      dir = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return false;
    }
    File dirLic = new File(dir, "licenses");
    if (!dirLic.exists() || dirLic.isFile() || !dirLic.canRead()) {
      return false;
    }
    File lic = new File(dirLic, id);
    if (!lic.exists() || !lic.isFile() || !lic.canRead()) {
      return false;
    }

    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    try {
      is = new FileInputStream(lic);
      isr = new InputStreamReader(is);
      br = new BufferedReader(isr);
      String line = br.readLine();
      return line.equals(Utils.hash(id, "SHA-224"));
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (isr != null) {
        try {
          isr.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return false;
  }
}
